<script setup>
import { ElContainer, ElHeader, ElAside, ElMain, ElDropdown, ElDropdownMenu, ElDropdownItem, ElAvatar } from 'element-plus'
import {
  Document,
  Menu as IconMenu,
  Edit,
  Setting,
  User,
  OfficeBuilding,
  Tickets,
  SetUp,
  ArrowDown
} from '@element-plus/icons-vue'
import { ref, onMounted, watch } from 'vue'
import { RouterView, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()

// 用户数据
const userData = ref({
  name: '',
  position: '',
  avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
})

// 系统标题
const systemTitle = ref('美容美发管理系统')

// 菜单处理函数
const handleOpen = (key, keyPath) => {
  console.log(key, keyPath)
}
const handleClose = (key, keyPath) => {
  console.log(key, keyPath)
}

// 当前激活的菜单项
const activeMenu = ref('')

// 默认展开的菜单
const defaultOpeneds = ref(['1', '2', '3', '4', '5', '6'])

// 监听路由变化
const updateActiveMenu = () => {
  activeMenu.value = router.currentRoute.value.path
}

// 设置菜单处理函数
const handleCommand = (command) => {
  if (command === 'logout') {
    // 退出登录逻辑
    localStorage.removeItem('token')
    localStorage.removeItem('user')
    ElMessage.success('退出登录成功')
    router.push('/login')
  } else if (command === 'editInfo') {
    console.log('编辑个人信息')
    // 这里添加编辑个人信息的逻辑
  }
}

// 用户角色
const userRole = ref('')

// 根据角色判断是否有权限
const hasPermission = (role, requiredRoles) => {
  return requiredRoles.includes(role)
}

// 初始化用户信息
onMounted(() => {
  const userStr = localStorage.getItem('user')
  if (userStr) {
    const user = JSON.parse(userStr)
    userData.value.name = user.username
    userData.value.position = user.role
    userRole.value = user.role
  } else {
    // 如果没有用户信息，重定向到登录页
    router.push('/login')
  }
  
  // 初始化菜单状态
  updateActiveMenu()
  
  // 监听路由变化
  watch(() => router.currentRoute.value.path, () => {
    updateActiveMenu()
  })
})
</script>

<template>
  <div class="common-layout">
    <el-container>
      <el-header class="custom-header">
        <div class="header-wrapper">
          <!-- Logo区域 -->
          <div class="logo-area">
            <div class="logo">LOGO</div>
          </div>

          <!-- 标题区域 -->
          <div class="title-area">
            <h1>{{ systemTitle }}</h1>
          </div>

          <!-- 用户信息区域 -->
          <div class="user-info">
            <el-avatar :size="40" :src="userData.avatar" />
            <div class="user-details">
              <div class="user-name">{{ userData.name }}</div>
              <div class="user-position">{{ userData.position }}</div>
            </div>

            <!-- 设置图标和下拉菜单 -->
            <el-dropdown @command="handleCommand" trigger="click">
              <div class="settings-icon">
                <el-icon>
                  <Setting />
                </el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="editInfo">编辑信息</el-dropdown-item>
                  <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu active-text-color="#ffd04b" background-color="#545c64" class="el-menu-vertical-demo"
            :default-active="activeMenu" :default-openeds="defaultOpeneds" text-color="#fff" @open="handleOpen" @close="handleClose" router>

            <!-- 门店管理 - 老板和管理员可见 -->
            <el-sub-menu index="1" v-if="hasPermission(userRole, ['老板', '管理员'])">
              <template #title>
                <el-icon>
                  <OfficeBuilding />
                </el-icon>
                <span>门店管理</span>
              </template>
              <el-menu-item index="/layout/lookStore">查看门店</el-menu-item>
              <el-menu-item index="/layout/manageStore" v-if="hasPermission(userRole, ['老板'])">管理门店</el-menu-item>
            </el-sub-menu>

            <!-- 人员管理 - 老板和管理员可见 -->
            <el-sub-menu index="2" v-if="hasPermission(userRole, ['老板', '管理员'])">
              <template #title>
                <el-icon>
                  <User />
                </el-icon>
                <span>人员管理</span>
              </template>
              <el-menu-item index="/layout/lookperson">查看人员</el-menu-item>
              <el-menu-item index="/layout/manageperson"
                v-if="hasPermission(userRole, ['老板', '管理员'])">管理人员</el-menu-item>
            </el-sub-menu>

            <!-- 订单管理 - 所有角色可见，但管理功能只有老板和管理员可用 -->
            <el-sub-menu index="3">
              <template #title>
                <el-icon>
                  <Edit />
                </el-icon>
                <span>订单管理</span>
              </template>
              <el-menu-item index="3-1">查看订单</el-menu-item>
              <el-menu-item index="3-2" v-if="hasPermission(userRole, ['老板', '管理员'])">管理订单</el-menu-item>
            </el-sub-menu>

            <!-- 服务管理 - 所有角色可见，但管理功能只有老板和管理员可用 -->
            <el-sub-menu index="4">
              <template #title>
                <el-icon>
                  <Tickets />
                </el-icon>
                <span>服务管理</span>
              </template>
              <el-menu-item index="4-1">查看服务</el-menu-item>
              <el-menu-item index="4-2" v-if="hasPermission(userRole, ['老板', '管理员'])">管理服务</el-menu-item>
            </el-sub-menu>

            <!-- 客户管理 - 所有角色可见，但管理功能只有老板和管理员可用 -->
            <el-sub-menu index="5">
              <template #title>
                <el-icon>
                  <Edit />
                </el-icon>
                <span>客户管理</span>
              </template>
              <el-menu-item index="5-1">查看客户</el-menu-item>
              <el-menu-item index="5-2">客户预约记录</el-menu-item>
              <el-menu-item index="5-3" v-if="hasPermission(userRole, ['老板', '管理员'])">客户评价管理</el-menu-item>
            </el-sub-menu>

            <!-- 排班管理 - 老板和管理员可见 -->
            <el-sub-menu index="6" v-if="hasPermission(userRole, ['老板', '管理员', '员工'])">
              <template #title>
               <el-icon><SetUp /></el-icon>
                <span>排班管理</span>
              </template>
              <el-menu-item index="/schedule-management">查看排班</el-menu-item>
            </el-sub-menu>

            <!-- 统计分析 - 老板和管理员可见 -->
            <el-sub-menu index="7" v-if="hasPermission(userRole, ['老板', '管理员'])">
              <template #title>
                <el-icon>
                  <SetUp />
                </el-icon>
                <span>统计分析</span>
              </template>
              <el-menu-item index="6-1">订单统计</el-menu-item>
              <el-menu-item index="/layout/serviceStats">服务统计</el-menu-item>
              <el-menu-item index="6-3">客户统计</el-menu-item>
            </el-sub-menu>

            <!-- 系统设置 - 只有老板可见 -->
            <el-menu-item index="7" v-if="hasPermission(userRole, ['老板'])">
              <template #title>
                <el-icon>
                  <Setting />
                </el-icon>
                <span>系统设置</span>
              </template>
            </el-menu-item>

          </el-menu>
        </el-aside>
        <el-main>
          <RouterView />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<style>
/* 头部样式 */
.custom-header {
  background-color: #304156;
  color: white;
  padding: 0;
  height: 60px;
}

.header-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
}

/* Logo区域样式 */
.logo-area {
  width: 120px;
}

.logo {
  background-color: #2b9939;
  color: white;
  padding: 5px 10px;
  border-radius: 4px;
  display: inline-block;
  font-weight: bold;
}

/* 标题区域样式 */
.title-area {
  flex-grow: 1;
  text-align: center;
}

.title-area h1 {
  margin: 0;
  font-size: 20px;
}

/* 用户信息区域样式 */
.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-details {
  margin-right: 10px;
}

.user-name {
  font-size: 14px;
  font-weight: bold;
}

.user-position {
  font-size: 12px;
  color: #e0e0e0;
}

.settings-icon {
  cursor: pointer;
  padding: 5px;
}

/* 侧边栏样式 */
.el-menu-vertical-demo {
  height: 100%;
  border-right: none;
}

.el-aside {
  background-color: #545c64;
}

.el-main {
  padding: 20px;
  background-color: #f5f7fa;
}
</style>
